//
//  LZMsgCenterPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/13.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZMsgCenterPresenter.h"
#import "NetworkServiceManager.h"
#import "MJExtension.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"
#import "LAReviewModel.h"

#import "LZMessageModel.h"

extern NSString *merchant;
static NSString *const kMsgListURL = @"/msg/list"; //消息列表
static NSString *const kHaveUnReadMsgURL = @"/msg/hasUnReadMsg"; //是否有未读消息
static NSString *const kDeleteMsgURL = @"/msg/delete"; //删除消息
static NSString *const kVideoCommentURL= @"/comment/videoCommentDetail"; //视频评论详情
static NSString *const kResCommentURL = @"/comment/merchantCommentDetail"; // 视频评论详情
static NSString *const kMsgDetailURL = @"/msg/info"; //消息详情

@implementation LZMsgCenterPresenter

#pragma mark - 消息列表
- (void)getMsgListWithTimeStamp:(NSTimeInterval)timeStamp Page:(NSInteger)page Limit:(NSInteger)limit Success:(void (^)(NSArray *, NSTimeInterval, NSInteger, NSInteger))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant, kMsgListURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:[NSNumber numberWithDouble:timeStamp] forKey:@"timestamp"];
    [dic setObject:[NSNumber numberWithInteger:page] forKey:@"cpage"];
    [dic setObject:[NSNumber numberWithInteger:limit] forKey:@"limit"];
    [dic setObject:[LZUserModel Instance].userId forKey:@"userId"];
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            NSInteger totalPageTemp = [resultData[@"data"][@"totalPages"] integerValue];
            NSInteger currentPageTemp = [resultData[@"data"][@"cPageNO"] integerValue];
            NSTimeInterval timeStampTemp = [resultData[@"timestamp"] doubleValue];
            NSMutableArray *modelArr = [[NSMutableArray alloc] init];
            NSArray *arr = resultData[@"data"][@"list"];
            for (NSInteger i = 0; i< arr.count; i ++) {
                LZMessageModel *m = [LZMessageModel mj_objectWithKeyValues:arr[i]];
                [modelArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(modelArr,timeStampTemp,totalPageTemp,currentPageTemp);
            }
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 消息详情--调用后不做操作.只为了后台能够统计消息是否已读.调用后代表已读
- (void)getMsgDetailWithMsgId:(NSString *)msgId Success:(PresenterCommonSuccess)successblock Failure:(PresenterCommonFail)failureBlock
{
    if (msgId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kMsgDetailURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:msgId forKey:@"id"];
    [dic setObject:[LZUserModel Instance].userId forKey:@"userId"];
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager]executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successblock) {
                successblock(resultData);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 是否有未读消息
- (void)checkWetherHaveUnReadMsgSuccess:(void (^)(NSInteger))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kHaveUnReadMsgURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:[LZUserModel Instance].userId forKey:@"userId"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            NSInteger count = [resultData[@"data"] integerValue];
            if (successBlock) {
                successBlock(count);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark - 删除消息
- (void)deleteMsgWithID:(NSString *)msgId Success:(PresenterCommonSuccess)successblock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kDeleteMsgURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:msgId forKey:@"id"];
    [dic setObject:[LZUserModel Instance].sellerId forKey:@"userId"];
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successblock) {
                successblock(nil);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];

}

#pragma mark - 餐厅评论详情
- (void)getRestaurantCommentDetailWithCommentID:(NSString *)commentId Success:(void(^)(LAReviewModel *model))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kResCommentURL);
    NSDictionary *dic = @{@"commentId" : commentId};
    
    [[NetworkServiceManager manager]executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            LAReviewModel *m = [LAReviewModel mj_objectWithKeyValues:resultData[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];

}

#pragma mark - 视频评论详情
- (void)getVideoCommentDetailWithCommentID:(NSString *)commentId Success:(void(^)(LAReviewModel *model))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kVideoCommentURL);
    NSDictionary *dic = @{@"commentId" : commentId};
    
    [[NetworkServiceManager manager]executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            LAReviewModel *m = [LAReviewModel mj_objectWithKeyValues:resultData[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

@end
